package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.cache.service.ICommunityRankVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.CommunityRankVO;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.community.domain.CommunityAnswer;
import cn.wolfcode.luowowo.community.domain.CommunityQuestion;
import cn.wolfcode.luowowo.community.domain.CommunityQuestionTag;
import cn.wolfcode.luowowo.community.service.ICommunityAnswerService;
import cn.wolfcode.luowowo.community.service.ICommunityQuestionService;
import cn.wolfcode.luowowo.community.service.ICommunityQuestionTagService;
import cn.wolfcode.luowowo.community.service.ICommunityQuestionVOService;
import cn.wolfcode.luowowo.community.vo.CommunityQuestionVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/community")
public class CommunityController {

    @Reference
    private ICommunityQuestionVOService communityQuestionVOService;

    @Reference
    private ICommunityAnswerService answerService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private ICommunityQuestionTagService communityQuestionTagService;

    @Reference
    private ICommunityQuestionService communityQuestionService;

    @Reference
    private ICommunityRankVOService communityRankVOService;

    /**
     * 社区问答的排行榜
     * @return
     */
    @RequestMapping("/rank")
    public String rank(Model model, CommunityRankVO rankVO) {

        List<CommunityRankVO> vos = communityRankVOService.getTop10(rankVO);
        model.addAttribute("vos", vos);
        return "/community/rankTpl";
    }

    // 往社区回复文档里添加数据,测试用
    @RequestMapping("/addData")
    @ResponseBody
    public void addData() {
        CommunityAnswer answer = new CommunityAnswer();
        answer.setContent("我是一条回复");
        answer.setCity("广州");
        answer.setDestId(3261L);
        answer.setDestName("广州");
        answer.setHeadImgUrl("/images/default.jpg");
        answer.setLevel(11);
        answer.setQuestionId(1L);
        answer.setId("2");
        answer.setReleaseTime(new Date());
        answer.setType(0);
        answer.setReplynum(11);
        answer.setViewnum(324);
        answer.setThumbnum(100);
        answer.setUserId(4L);
        answer.setUsername("dafei");
        answerService.save(answer);
    }

    /**
     * 问题列表
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model) {

        // 先查出所有的CommunityQuestionVO对象
        List<CommunityQuestionVO> questionVOS = communityQuestionVOService.query();
        model.addAttribute("questionVOS", questionVOS);

        return "community/wenda";
    }

    /**
     * 跳转到发起提问界面
     * @param model
     * @return
     */
    @RequestMapping("/question/input")
    @RequireLogin
    public String addQuestion(Model model) {

        // 查询所有地区deep为3的地区
        List<Destination> destinations = destinationService.getDestsByDeep(3);
        model.addAttribute("destinations", destinations);

        // 查出所有标签
        List<CommunityQuestionTag> tags = communityQuestionTagService.query();
        model.addAttribute("tags", tags);

        return "community/public";
    }

    /**
     * 保存提问
     * @param question
     * @param userInfo
     * @return
     */
    @RequestMapping("/question/save")
    //@ResponseBody
    @RequireLogin
    public String saveQuestion(CommunityQuestion question, @UserParam UserInfo userInfo) {
        communityQuestionService.save(question, userInfo);

        return "redirect:/community/list";
    }

    /**
     * 查询单条问题及其回答
     * @param model
     * @param id
     * @param selectAll
     * @return
     */
    @RequestMapping("/question")
    public String question(Model model, Long id, boolean selectAll) {

        // 先同过问题的id查出问题的信息
        CommunityQuestion question = communityQuestionService.get(id);
        model.addAttribute("question", question);

        // 查回答数
        int count = answerService.QueryForCount(id);
        model.addAttribute("count", count);

        // 查出金牌回答的相关信息
        CommunityAnswer goldReply = answerService.queryGoldReplyByQuestionId(id);
        model.addAttribute("goldReply",goldReply);

        // 如果需要查全部, 将剩下的那些回复查询出来
        if (selectAll) {
            List<CommunityAnswer> answers = answerService.QueryAllButGoldReply(id);
            model.addAttribute("answers", answers);
        }

        return "community/wendaDetail";
    }

    /**
     * 回复问题
     * @return
     */
    @RequestMapping("/question/addReply")
    @RequireLogin
    @ResponseBody
    public AjaxResult addReply(Long questionId, @UserParam UserInfo userInfo, String content) {
        if (content==null) {
            return new AjaxResult(false, "您未进行任何评论");
        }
        answerService.addReply(questionId, userInfo, content);

        // 对应的问题评论数+1
        communityQuestionService.addReplynum(questionId, 1);

        // 往3个评论的zset中对应的值加分
        communityRankVOService.addScore(RedisKeys.COMMUNITY_RANK_DAY_ANSWER,userInfo.getId(), 1);
        communityRankVOService.addScore(RedisKeys.COMMUNITY_RANK_WEEK_ANSWER,userInfo.getId(), 1);
        communityRankVOService.addScore(RedisKeys.COMMUNITY_RANK_MONTH_ANSWER,userInfo.getId(), 1);

        return new AjaxResult(true);
    }

    /**
     * 浏览数增加
     */
    @RequestMapping("/addQuestionViewnum")
    @ResponseBody
    public AjaxResult addQuestionViewnum(Long questionId) {
        communityQuestionService.addQuestionViewnum(questionId,1);
        return AjaxResult.success();
    }

    /**
     * 顶的方法
     */
    @RequestMapping("/answer/thumb")
    @RequireLogin
    @ResponseBody
    public AjaxResult thumb(Long questionId, String answerId, Long answerUserId , @UserParam UserInfo userInfo) {

        if (userInfo.getId().equals(answerUserId)) {
            return new AjaxResult(false, "不能顶自己的回复哦");
        }

        // 应该判断当前用户当天有没有顶过着条回复,先不做

        // 回答的顶的数量加1,可以数据落地的时候再处理
        answerService.addThumb(answerId, 1);

        // 对应的顶的分数加1
        communityRankVOService.addScore(RedisKeys.COMMUNITY_RANK_DAY_THUMB,answerUserId,1);
        communityRankVOService.addScore(RedisKeys.COMMUNITY_RANK_WEEK_THUMB,answerUserId,1);
        communityRankVOService.addScore(RedisKeys.COMMUNITY_RANK_MONTH_THUMB,answerUserId,1);

        return AjaxResult.success();
    }

}
